﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;
using SF.Common;//Please add references
namespace SF.DAL
{
	/// <summary>
	/// 数据访问类:sf_user_info
	/// </summary>
	public partial class sf_user_info
	{
		public sf_user_info()
		{}
		#region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_user_info");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByKey(string inviteKey)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_user_info");
            strSql.Append(" where fatherKey=@fatherKey");
            SqlParameter[] parameters = {
					new SqlParameter("@fatherKey", SqlDbType.NVarChar)
			};
            parameters[0].Value = inviteKey;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByNum(string userNum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_user_info");
            strSql.Append(" where userNum=@userNum");
            SqlParameter[] parameters = {
					new SqlParameter("@userNum", SqlDbType.NVarChar)
			};
            parameters[0].Value = userNum;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByOpenId(string openid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from sf_user_info");
            strSql.Append(" where openID=@openID");
            SqlParameter[] parameters = {
					new SqlParameter("@openID", SqlDbType.NVarChar)
			};
            parameters[0].Value = openid;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(SF.Model.sf_user_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sf_user_info(");
            strSql.Append("busnieseNum,appNum,templatesNum,userNum,nickName,province,city,addTime,openID,photoUri,isAttention,sellModel,inviteKey,fatherKey)");
			strSql.Append(" values (");
            strSql.Append("@busnieseNum,@appNum,@templatesNum,@userNum,@nickName,@province,@city,@addTime,@openID,@photoUri,@isAttention,@sellModel,@inviteKey,@fatherKey)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@nickName", SqlDbType.NVarChar,100),
					new SqlParameter("@province", SqlDbType.NVarChar,30),
					new SqlParameter("@city", SqlDbType.NVarChar,30),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@openID", SqlDbType.NVarChar,100),
					new SqlParameter("@photoUri", SqlDbType.NVarChar,150),
					new SqlParameter("@isAttention", SqlDbType.Int,4),
					new SqlParameter("@inviteKey", SqlDbType.NVarChar,30),
					new SqlParameter("@fatherKey", SqlDbType.NVarChar,30),
					new SqlParameter("@sellModel", SqlDbType.Int,4)};
			parameters[0].Value = model.busnieseNum;
			parameters[1].Value = model.appNum;
			parameters[2].Value = model.templatesNum;
			parameters[3].Value = model.userNum;
			parameters[4].Value = model.nickName;
			parameters[5].Value = model.province;
			parameters[6].Value = model.city;
			parameters[7].Value = model.addTime;
			parameters[8].Value = model.openID;
            parameters[9].Value = model.photoUri;
            parameters[10].Value = model.isAttention;
            parameters[11].Value = model.inviteKey;
            parameters[12].Value = model.fatherKey;
			parameters[13].Value = model.sellModel;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_user_info model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sf_user_info set ");
			strSql.Append("busnieseNum=@busnieseNum,");
			strSql.Append("appNum=@appNum,");
			strSql.Append("templatesNum=@templatesNum,");
			strSql.Append("userNum=@userNum,");
			strSql.Append("nickName=@nickName,");
			strSql.Append("province=@province,");
			strSql.Append("city=@city,");
			strSql.Append("addTime=@addTime,");
			strSql.Append("openID=@openID,");
			strSql.Append("photoUri=@photoUri,");
			strSql.Append("isAttention=@isAttention,");
			strSql.Append("sellModel=@sellModel");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@nickName", SqlDbType.NVarChar,100),
					new SqlParameter("@province", SqlDbType.NVarChar,30),
					new SqlParameter("@city", SqlDbType.NVarChar,30),
					new SqlParameter("@addTime", SqlDbType.DateTime),
					new SqlParameter("@openID", SqlDbType.NVarChar,100),
					new SqlParameter("@photoUri", SqlDbType.NVarChar,150),
					new SqlParameter("@isAttention", SqlDbType.Int,4),
					new SqlParameter("@sellModel", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.busnieseNum;
			parameters[1].Value = model.appNum;
			parameters[2].Value = model.templatesNum;
			parameters[3].Value = model.userNum;
			parameters[4].Value = model.nickName;
			parameters[5].Value = model.province;
			parameters[6].Value = model.city;
			parameters[7].Value = model.addTime;
			parameters[8].Value = model.openID;
			parameters[9].Value = model.photoUri;
			parameters[10].Value = model.isAttention;
			parameters[11].Value = model.sellModel;
			parameters[12].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sf_user_info ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sf_user_info ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_user_info GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,userNum,nickName,province,city,addTime,openID,photoUri,isAttention,sellModel,inviteKey,fatherKey from sf_user_info ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
			parameters[0].Value = id;

			SF.Model.sf_user_info model=new SF.Model.sf_user_info();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_user_info GetModelByNum(string userNum, string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,userNum,nickName,province,city,addTime,openID,photoUri,isAttention,sellModel,inviteKey,fatherKey from sf_user_info ");
            strSql.Append(" where userNum=@userNum");
            if (where!="")
            {
                strSql.Append(" and " + where);
            }
            SqlParameter[] parameters = {
					new SqlParameter("@userNum", SqlDbType.NVarChar)
			};
            parameters[0].Value = userNum;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_user_info DataRowToModel(DataRow row)
		{
			SF.Model.sf_user_info model=new SF.Model.sf_user_info();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["busnieseNum"]!=null)
				{
					model.busnieseNum=row["busnieseNum"].ToString();
				}
				if(row["appNum"]!=null)
				{
					model.appNum=row["appNum"].ToString();
				}
				if(row["templatesNum"]!=null)
				{
					model.templatesNum=row["templatesNum"].ToString();
				}
				if(row["userNum"]!=null)
				{
					model.userNum=row["userNum"].ToString();
				}
				if(row["nickName"]!=null)
				{
					model.nickName=row["nickName"].ToString();
				}
				if(row["province"]!=null)
				{
					model.province=row["province"].ToString();
				}
				if(row["city"]!=null)
				{
					model.city=row["city"].ToString();
				}
				if(row["addTime"]!=null && row["addTime"].ToString()!="")
				{
					model.addTime=DateTime.Parse(row["addTime"].ToString());
				}
				if(row["openID"]!=null)
				{
					model.openID=row["openID"].ToString();
				}
				if(row["photoUri"]!=null)
				{
					model.photoUri=row["photoUri"].ToString();
				}
				if(row["isAttention"]!=null && row["isAttention"].ToString()!="")
				{
					model.isAttention=int.Parse(row["isAttention"].ToString());
				}
				if(row["sellModel"]!=null && row["sellModel"].ToString()!="")
				{
					model.sellModel=int.Parse(row["sellModel"].ToString());
                }
                if (row["inviteKey"] != null)
                {
                    model.inviteKey = row["inviteKey"].ToString();
                }
                if (row["fatherKey"] != null)
                {
                    model.fatherKey = row["fatherKey"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,busnieseNum,appNum,templatesNum,userNum,nickName,province,city,addTime,openID,photoUri,isAttention,sellModel,inviteKey,fatherKey ");
			strSql.Append(" FROM sf_user_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,busnieseNum,appNum,templatesNum,userNum,nickName,province,city,addTime,openID,photoUri,isAttention,sellModel,inviteKey,fatherKey ");
			strSql.Append(" FROM sf_user_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sf_user_info ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from sf_user_info T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM sf_user_info");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 根据openid返回一个实体
        /// </summary>
        public Model.sf_user_info GetModelByOpenId(string openid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from sf_user_info");
            strSql.Append(" where openID=@openID");
            SqlParameter[] parameters = {
					new SqlParameter("@openID", SqlDbType.NVarChar,250)};
            parameters[0].Value = openid;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 根据邀请码返回一个实体
        /// </summary>
        public Model.sf_user_info GetModelByKey(string inviteKey,string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id from sf_user_info");
            strSql.Append(" where inviteKey=@inviteKey");
            SqlParameter[] parameters = {
					new SqlParameter("@inviteKey", SqlDbType.NVarChar,20)};
            parameters[0].Value = inviteKey;
            if (where!="")
            {
                strSql.Append(" and "+where);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return GetModel(Convert.ToInt32(obj));
            }
            return null;
        }

        /// <summary>
        /// 关注时 获取当前 用户数量
        /// </summary>
        public long getUserCount(string where)
        {
            string sql = "select count(id) from sf_user_info where "+where;
            return Convert.ToInt32(DbHelperSQL.GetSingle(sql));
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string strValue, string where)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update sf_user_info set " + strValue);
                strSql.Append(" where " + where);
                DbHelperSQL.ExecuteSql(strSql.ToString());
                return true;
            }
            catch (Exception)
            {
            }
            return false;
        }
		#endregion  ExtensionMethod
	}
}

